package com.ruoyi.controller.purchase;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.purchase.MesPurchaseReceipt;
import com.ruoyi.service.purchase.IMesPurchaseReceiptService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购入库Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/purchase/receipt")
public class MesPurchaseReceiptController extends BaseController
{
    @Autowired
    private IMesPurchaseReceiptService mesPurchaseReceiptService;

    /**
     * 查询采购入库列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesPurchaseReceipt mesPurchaseReceipt)
    {
        startPage();
        List<MesPurchaseReceipt> list = mesPurchaseReceiptService.selectMesPurchaseReceiptList(mesPurchaseReceipt);
        return getDataTable(list);
    }

    /**
     * 查询已入库订单列表（历史记录）
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:list')")
    @GetMapping("/receivedList")
    public TableDataInfo receivedList(MesPurchaseReceipt mesPurchaseReceipt)
    {
        startPage();
        List<MesPurchaseReceipt> list = mesPurchaseReceiptService.selectReceivedReceiptList(mesPurchaseReceipt);
        return getDataTable(list);
    }

    /**
     * 导出采购入库列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:export')")
    @Log(title = "采购入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesPurchaseReceipt mesPurchaseReceipt)
    {
        List<MesPurchaseReceipt> list = mesPurchaseReceiptService.selectMesPurchaseReceiptList(mesPurchaseReceipt);
        ExcelUtil<MesPurchaseReceipt> util = new ExcelUtil<MesPurchaseReceipt>(MesPurchaseReceipt.class);
        util.exportExcel(response, list, "采购入库数据");
    }

    /**
     * 获取采购入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:query')")
    @GetMapping(value = "/{receiptId}")
    public AjaxResult getInfo(@PathVariable("receiptId") Long receiptId)
    {
        return success(mesPurchaseReceiptService.selectMesPurchaseReceiptByReceiptId(receiptId));
    }

    /**
     * 新增采购入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:add')")
    @Log(title = "采购入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesPurchaseReceipt mesPurchaseReceipt)
    {
        return toAjax(mesPurchaseReceiptService.insertMesPurchaseReceipt(mesPurchaseReceipt));
    }

    /**
     * 修改采购入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:edit')")
    @Log(title = "采购入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesPurchaseReceipt mesPurchaseReceipt)
    {
        return toAjax(mesPurchaseReceiptService.updateMesPurchaseReceipt(mesPurchaseReceipt));
    }

    /**
     * 删除采购入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:remove')")
    @Log(title = "采购入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{receiptIds}")
    public AjaxResult remove(@PathVariable Long[] receiptIds)
    {
        return toAjax(mesPurchaseReceiptService.deleteMesPurchaseReceiptByReceiptIds(receiptIds));
    }

    /**
     * 审核采购入库单
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:approve')")
    @Log(title = "审核采购入库", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{receiptId}")
    public AjaxResult approve(@PathVariable Long receiptId)
    {
        return toAjax(mesPurchaseReceiptService.approveMesPurchaseReceipt(receiptId));
    }

    /**
     * 处理质检结果
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:inspection')")
    @Log(title = "处理质检结果", businessType = BusinessType.UPDATE)
    @PutMapping("/handleInspection")
    public AjaxResult handleInspectionResult(@RequestBody MesPurchaseReceipt mesPurchaseReceipt)
    {
        return toAjax(mesPurchaseReceiptService.handleInspectionResult(mesPurchaseReceipt.getReceiptId(), 
            mesPurchaseReceipt.getStatus(), mesPurchaseReceipt.getRemark()));
    }

    /**
     * 执行入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:execute')")
    @Log(title = "执行入库", businessType = BusinessType.UPDATE)
    @PutMapping("/execute/{receiptId}")
    public AjaxResult execute(@PathVariable Long receiptId)
    {
        return toAjax(mesPurchaseReceiptService.executeMesPurchaseReceipt(receiptId));
    }

    /**
     * 根据供应商ID查询入库单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:list')")
    @GetMapping("/supplier/{supplierId}")
    public AjaxResult getReceiptListBySupplierId(@PathVariable Long supplierId)
    {
        List<MesPurchaseReceipt> list = mesPurchaseReceiptService.selectReceiptListBySupplierId(supplierId);
        return success(list);
    }

    /**
     * 根据仓库ID查询入库单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:list')")
    @GetMapping("/warehouse/{warehouseId}")
    public AjaxResult getReceiptListByWarehouseId(@PathVariable Long warehouseId)
    {
        List<MesPurchaseReceipt> list = mesPurchaseReceiptService.selectReceiptListByWarehouseId(warehouseId);
        return success(list);
    }

    /**
     * 根据状态查询入库单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:receipt:list')")
    @GetMapping("/status/{status}")
    public AjaxResult getReceiptListByStatus(@PathVariable String status)
    {
        List<MesPurchaseReceipt> list = mesPurchaseReceiptService.selectReceiptListByStatus(status);
        return success(list);
    }
}